home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / et / et3_0-a1.lha / et3 / src / ProgressDialog.C < prev    next >
C/C++ Source or Header  |  1992-07-08  |  4KB  |  206 lines

  1. #ifdef __GNUG__
  2. #pragma implementation
  3. #endif
  4.  
  5. #include "ProgressDialog.h"
  6.  
  7. #include "Class.h"
  8. #include "Dialog.h"
  9. #include "Expander.h"
  10. #include "TextItem.h"
  11. #include "WindowSystem.h"
  12. #include "Window.h"
  13. #include "WindowPort.h"
  14. #include "Buttons.h"
  15. #include "String.h"
  16. #include "Look.h"
  17. #include "BorderItems.h"
  18. #include "Alert_e.h"
  19. #include "CLib.h"
  20.  
  21. //---- AlarmHandler ------------------------------------------------------------
  22.  
  23. class AlarmHandler : public SysEvtHandler {
  24.     ProgressImpl *progress;
  25. public:
  26.     MetaDef(AlarmHandler);
  27.     AlarmHandler(ProgressImpl *pi) : SysEvtHandler(eSigAlarm)
  28.     { progress= pi; }
  29.     void Notify(SysEventCodes, int)
  30.     { progress->timeout= TRUE; }
  31. };
  32.  
  33. NewMetaImpl0(AlarmHandler, SysEvtHandler);
  34.  
  35. //---- ProgressBar -------------------------------------------------------------
  36.  
  37. class ProgressBar : public VObject {
  38. public:
  39.     int val;
  40. public:
  41.     ProgressBar() : VObject(Point(20))
  42.     { }
  43.     Metric GetMinSize()
  44.     { return gLook->ProgressBarLayout()->GetMinSize(this);  }
  45.     void Draw(Rectangle r)
  46.     { gLook->ProgressBarLayout()->Adorn(this, r, val); }
  47.     void SetVal(int v);
  48. };
  49.  
  50. void ProgressBar::SetVal(int v)
  51. {
  52.     val= v;
  53.     ForceRedraw();
  54.     if (GetWindow()) {
  55.     WindowPort *wp= (WindowPort*) GetWindow()->GetPortDesc();
  56.     wp->DevUpdate();
  57.     }
  58. }
  59.  
  60. //---- ProgressDialog ----------------------------------------------------------
  61.  
  62. const int cIdStop   = cIdFirstUser + 100;
  63.  
  64. class ProgressDialog : public Dialog {
  65.     ProgressBar *bar;
  66.     TextItem *ti;
  67. public:
  68.     bool quit;
  69.     
  70.     VObject *DoMakeContent();
  71. public:
  72.     MetaDef(ProgressDialog);
  73.     ProgressDialog(ProgressBar *b, TextItem *t) : Dialog("Progress", eWinFixed)
  74.     { bar= b; ti= t; }
  75.     void Control(int id, int part, void *val);
  76.     void Show();
  77. };
  78.  
  79. NewMetaImpl0(ProgressDialog, Dialog);
  80.  
  81. void ProgressDialog::Show()
  82. {
  83.     quit= FALSE;
  84.     bar->val= 0;
  85.     ShowAt(gWindow, gWindow->contentRect.Center());
  86. }
  87.  
  88. VObject *ProgressDialog::DoMakeContent()
  89. {
  90.     return
  91.     new Matte(
  92.         new VExpander(gPoint10,
  93.         ti,
  94.         bar,
  95.         new ActionButton(cIdStop, "Stop"),
  96.         0
  97.         )
  98.     );
  99. }
  100.  
  101. void ProgressDialog::Control(int id, int part, void *val)
  102. {
  103.     if (id == cIdStop)
  104.     quit= TRUE;
  105.     else
  106.     Dialog::Control(id, part, val);
  107. }
  108.  
  109. //---- ProgressImpl ------------------------------------------------------------
  110.  
  111. ProgressImpl::ProgressImpl()
  112. {
  113. }
  114.  
  115. ProgressImpl::~ProgressImpl()
  116. {
  117.     if (pb == 0)
  118.     SafeDelete(ti);
  119.     SafeDelete(pb);
  120. }
  121.  
  122. void ProgressImpl::SetUp()
  123. {
  124.     gWindowSystem->SetWait();
  125.     if (alarmhandler == 0)
  126.     gSystem->AddAsyncSignalHandler(alarmhandler= new AlarmHandler(this));
  127.     CLib::Alarm(1);
  128. }
  129.  
  130. void ProgressImpl::SetMessage(char *message)
  131. {
  132.     if (ti)
  133.     ti->SetString(message);
  134.     else
  135.     ti= new TextItem(message);
  136. }
  137.  
  138. bool ProgressImpl::SetVal()
  139. {
  140.     if ((pb == 0 || ! pb->IsOpen()) && curr > /* (max*2)/3 */ max) {
  141.     timeout= FALSE;
  142.     return FALSE;
  143.     }
  144.  
  145.     if (bar == 0)
  146.     bar= new ProgressBar();
  147.  
  148.     int v= curr*bar->contentRect.extent.x/max;
  149.     
  150.     if (v != bar->val) {
  151.     if (pb == 0)
  152.         pb= new ProgressDialog(bar, ti);
  153.     if (! pb->IsOpen()) {
  154.         bar->SetVal(v);
  155.         pb->ShowOnWindow(gWindow);
  156.     } else
  157.         bar->SetVal(v);
  158.     ((WindowPort*) pb->GetWindow()->GetPortDesc())->Poll(0);
  159.     if (pb->quit) {
  160.         pb->quit= FALSE;
  161.         
  162.         if (ShowAlert(eAlertStop, "Abort %s ?", ti->AsString()) == cIdYes) {
  163.         Abort(FALSE);
  164.         return TRUE;
  165.         }
  166.     }
  167.     }
  168.     return FALSE;
  169. }
  170.  
  171. void ProgressImpl::Abort(bool skiptoend)
  172. {
  173.     gWindowSystem->ResetWait();
  174.     CLib::Alarm(0);
  175.     if (pb && pb->IsOpen()) {
  176.     if (skiptoend)
  177.         for (int i= bar->val; i < bar->contentRect.extent.x; i++)
  178.         bar->SetVal(i);
  179.     pb->Close();
  180.     }
  181. }
  182.  
  183. //---- ProgressFilter ----------------------------------------------------------
  184.  
  185. ProgressFilter::ProgressFilter() : StreamBuf()
  186. {
  187. }
  188.  
  189. int ProgressFilter::Underflow(u_char*, int size)
  190. {
  191.     size= sb->salloc(&base, BUFSIZE);
  192.     if (size == EOF)
  193.     return EOF;
  194.     if (gProgress->Inc(size))
  195.     return EOF;
  196.     return size;
  197. }
  198.  
  199. int ProgressFilter::Overflow(u_char *bp, int size)
  200. {
  201.     if (gProgress->Inc(size))
  202.     return EOF;
  203.     return size= sb->sputn((char*)bp, size);
  204.  
  205.